package com.zw.yzk.kabu.data.repository;

import android.content.Context;

import com.zw.yzk.kabu.data.network.task.Task;
import com.zw.yzk.kabu.data.cache.store.MallBannerStore;
import com.zw.yzk.kabu.data.network.ServiceGenerator;
import com.zw.yzk.kabu.data.network.entity.MallBannerEntity;
import com.zw.yzk.kabu.data.network.service.MallBannerService;
import com.zw.yzk.kabu.data.utils.NetworkUtils;

import javax.inject.Inject;

import rx.Observable;
import rx.functions.Func1;

/**
 * Created by zhanwei on 2016/12/24.
 */

public class MallBannerRepository extends CommonRepository {

    @Inject
    MallBannerStore mallBannerStore;
    private Context context;

    @Inject
    public MallBannerRepository(Context context) {
        this.context = context;
    }

    @Override
    protected Observable commonObserve(Task task) {
        if (NetworkUtils.isConnected(context)) {
            return Observable.concat(Observable.just(mallBannerStore.getBanner()), super.commonObserve(task));
        } else {
            return Observable.just(mallBannerStore.getBanner());
        }
    }

    @Override
    protected Observable connectServer(Task task) {
        MallBannerService mallBannerService = ServiceGenerator.createService(MallBannerService.class);
        return mallBannerService.getBanner();
    }

    @Override
    protected Func1<MallBannerEntity, Observable> getServerData() {
        return new Func1<MallBannerEntity, Observable>() {
            @Override
            public Observable call(MallBannerEntity entity) {
                mallBannerStore.refreshMallBanner(entity);
                return Observable.just(entity);
            }
        };
    }

}
